package com.sskj.phoenixcoin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.allen.library.SuperTextView;
import com.lzy.okgo.OkGo;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.OnPullListener;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.base.GlideApp;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.APKVersionCodeUtils;
import com.sskj.lib.util.HideUtil;
import com.sskj.lib.util.TipUtil;
import com.sskj.phoenixcoin.activity.APpRechargeActivity;
import com.sskj.phoenixcoin.activity.AppBindBankActivity;
import com.sskj.phoenixcoin.activity.AppRecordActivity;
import com.sskj.phoenixcoin.activity.AppSetTpwdActivity;
import com.sskj.phoenixcoin.activity.AppTransferActivity;
import com.sskj.phoenixcoin.activity.AppWithdrawActivity;
import com.sskj.phoenixcoin.activity.LoginActivity;
import com.sskj.phoenixcoin.bean.AppBanlance;
import com.sskj.phoenixcoin.bean.UpdataBean;
import com.sskj.phoenixcoin.component.DaggerUserDataComponent;
import com.sskj.phoenixcoin.http.HttpConfig;
import com.sskj.phoenixcoin.presenter.MainPresenter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import sskj.lee.appupdatelibrary.BaseUpdateDialogFragment;
import sskj.lee.appupdatelibrary.BaseVersion;

@Route(path = RConfig.APP_MAIN)
public class MainActivity extends BaseActivity<MainPresenter> {

    @BindView(R.id.head_img)
    ImageView headImg;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.login_show)
    TextView loginShow;
    @BindView(R.id.mobile_tv)
    TextView mobileTv;
    @BindView(R.id.rate)
    TextView rate;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.img_ll)
    LinearLayout imgLl;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.main_smartRefresh)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.app_recharge)
    SuperTextView appRecharge;
    @BindView(R.id.app_withdraw)
    SuperTextView appWithdraw;
    @BindView(R.id.app_transfer)
    SuperTextView appTransfer;
    @BindView(R.id.app_edit_tpwd)
    SuperTextView appEditTpwd;
    @BindView(R.id.app_bind_bank)
    SuperTextView appBindBank;
    @BindView(R.id.app_exit_login)
    Button appExitLogin;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private AppBanlance appBanlance;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    public MainPresenter getPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        ARouter.getInstance().inject(this);
        tvTitle.setText("钱包");
        ivBack.setVisibility(View.GONE);
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                appEditTpwd.getLeftTextView().setText(userData.getPhoneixPwd().equals("1") ? "修改交易密码" : "设置交易密码");
                loginShow.setText(userData.getNickname());
                mobileTv.setText(HideUtil.getPhoneHide(userData.getTel()));
//                ImageUtil.setCircleImage(userData.getHeadUrl(), headImg);
                ImageUtil.setCircleImage(getResources().getDrawable(R.mipmap.app_logo), headImg);
                mPresenter.getBanlance(String.valueOf(userData.getId()));
            } else {
                loginShow.setText("请登录");
                mobileTv.setText("----");
                ImageUtil.setCircleImage(getResources().getDrawable(R.mipmap.app_logo), headImg);
                userData = null;
            }
        });
        ClickUtil.click(loginShow, () -> {  // 登录
            if (userData == null) {
                finish();
                LoginActivity.start(this);
            }
        });
        ClickUtil.click(appRecharge, () -> {
            if (checkLogin(userData)) {
                if (appBanlance != null) {
                    APpRechargeActivity.start(this, appBanlance.getExchange_rate());
                }
            } else {
                login();
            }

        });
        ClickUtil.click(appWithdraw, () -> {
            if (checkLogin(userData)) {
                if (appBanlance != null) {
                    AppWithdrawActivity.start(this, appBanlance.getUsable_fund(), appBanlance.getExchange_rate());
                }
            } else {
                login();
            }
        });
        ClickUtil.click(appTransfer, () -> {
            if (checkLogin(userData)) {
                if (appBanlance != null) {
                    AppTransferActivity.start(this, appBanlance.getUsable_fund());
                }
            } else {
                login();
            }
        });
        ClickUtil.click(appEditTpwd, () -> {
            if (checkLogin(userData)) {
                AppSetTpwdActivity.start(this);
            } else {
                login();
            }
        });
        ClickUtil.click(appBindBank, () -> {
            if (checkLogin(userData)) {
                AppBindBankActivity.start(this);
            } else {
                login();
            }
        });
        ClickUtil.click(appExitLogin, () -> {
            finish();
            userViewModel.clear();
            if (OkGo.getInstance().getCommonHeaders() != null) {
                OkGo.getInstance().getCommonHeaders().clear();
            }
            if (OkGo.getInstance().getCommonParams() != null) {
                OkGo.getInstance().getCommonParams().clear();
            }
            LoginActivity.start(this);
        });
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                if (userData != null) {
                    mPresenter.getBanlance(String.valueOf(userData.getId()));
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.updata(APKVersionCodeUtils.getVerName(this));
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    public void loginSuccess(AppBanlance userData) {
        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
        appBanlance = userData;
        tv0.setText(userData.getUsable_fund());
        rate.setText(String.format("资产折合≈%1$sCNY(单价≈%2$sCNY)", userData.getFee(), NumberUtil.keep2(userData.getExchange_rate())));
    }

    public void login() {
        TipUtil.getLoginDialog(this, new TipUtil.OnTipSure() {
            @Override
            public void onSure() {
                finish();
                LoginActivity.start(MainActivity.this);
            }
        }).show();
    }


    public void needUpdata(UpdataBean updataBean) {
        if (updataBean == null) {
            return;
        }
//        if (updataBean.getUptype().equals("2")) {
//            updataBean.isMustUp();
//        }
        String verName = APKVersionCodeUtils.getVerName(this);
        updataBean.setViewStyle(BaseVersion.NOTIFYCATION_STYLE);
        if (APKVersionCodeUtils.compareVersion(updataBean.getVersion(), verName) == 1) {
            android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            AppUpDateFragment appUpDateFragment = new AppUpDateFragment();//自定义UI
            Bundle bundle = new Bundle();
            bundle.putSerializable(BaseUpdateDialogFragment.INTENT_KEY, updataBean);
            appUpDateFragment.setArguments(bundle);
            fragmentTransaction.add(appUpDateFragment, "updateUI");
            fragmentTransaction.commitAllowingStateLoss();
        } else {
        }

    }

}
